
#import "WBScrollSelectView.h"
@implementation WBScrollSelectView

/**
 *	@brief	唯一初始化函数
 *
 *	@param 	frame frame
 *	@param 	titles 	标题 数组属性
 *
 *	@return D
 */
- (id)initWithFrame:(CGRect)frame titles:(NSArray*)titles

{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        float buttonWith;
        if (titles.count<=4)
        {
            buttonWith = frame.size.width/titles.count;
        }
        else
        {
            buttonWith = frame.size.width/titles.count;
        }
        _LineWith= buttonWith;
        
        backScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        backScrView.backgroundColor = [UIColor whiteColor];
        backScrView.scrollEnabled = NO;
        [self addSubview:backScrView];
        
        lineImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frame.size.height-1, _LineWith, 1)];
        lineImgView.backgroundColor = kMainOriginColor;
        [self addSubview:lineImgView];
        
        UIView *upLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 0.5)];
        upLine.backgroundColor = kLineColor;
        [self addSubview:upLine];
        
        
        for (int i=0;i<titles.count;i++)
        {
            NSString *title = titles[i];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame  = CGRectMake(buttonWith*i, 0, buttonWith, frame.size.height);
            button.tag = 100+i;
            [button setTitle:title forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:16];
            [button setTitleColor:RGBCOLOR(47, 48, 49) forState:UIControlStateNormal];
            [button setTitleColor:kMainOriginColor forState:UIControlStateSelected];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [backScrView addSubview:button];
            backScrView.contentSize = CGSizeMake(buttonWith*(i+1), frame.size.height);
            if (i==0) {
                currentBtn = button;
                currentBtn.selected = YES;
            }
        }
    }
    return self;
}

- (void)setSelect:(int)select
{
    _selectIndex = select;
    currentBtn.selected = NO;
    UIButton *button = [backScrView viewWithTag:100+select ];
    button.selected = YES;
    currentBtn = button;
    
    [self moveDownLine:currentBtn];
}

- (void)setLineWith:(float)LineWith
{
    _LineWith = LineWith;
    
    if (LineWith==0) {
        
        lineImgView.hidden = YES;
    }
    else
    {
        lineImgView.hidden = NO;
        [self moveDownLine:currentBtn];
    }
   
}

- (void)moveDownLine:(UIButton*)button
{
    CGRect frame = lineImgView.frame;
    frame.size.width = self.LineWith;
    lineImgView.frame = frame;
    
    CGPoint center = lineImgView.center;
    center.x = button.center.x;
    lineImgView.center = center;
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
- (void)buttonClicked:(UIButton*)button
{
    //点击的是当前选择的按钮
    if (currentBtn==button) {
        
        return;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [self moveDownLine:button];
        
    } completion:^(BOOL finished) {
        
        self.selectIndex = button.tag-100;
        button.selected = YES;
        currentBtn.selected = NO;
        currentBtn = button;
        
        if ([self.delegate respondsToSelector:@selector(ScrollSelectDidCickWith:)])
        {
            [self.delegate ScrollSelectDidCickWith:button.tag-100];
        }
    }];
}

@end
